//
//  SYTVStatusViewModel.swift
//  SYWeibo
//
//  Created by Saya on 16/4/11.
//  Copyright © 2016年 黄金果. All rights reserved.
//

import UIKit

class SYTVStatusViewModel: NSObject {
    lazy var cellStatusViewModel:[SYCellStatusViewModel] = [SYCellStatusViewModel]()
    
    func loadHomeData(isPullUp:Bool,finished:(isDownLoaded:Bool)->()){
        guard let accessToken = SYVIewModel.viewModel.userInformation?.access_token else {
            print("用户的没有登录")
            return
        }
        
        var maxid:Int64 = 0
        var sinceid:Int64 = 0
        if isPullUp {
            maxid = cellStatusViewModel.last?.status?.id ?? 0
        }else {
            sinceid = cellStatusViewModel.first?.status?.id ?? 0
         }
        SYAFNTools.shareTools.loginHomeData(accessToken, max_id: maxid ,since_id: sinceid) { (reslut, error) -> Void in
            if error != nil {
                finished(isDownLoaded: false)
                return
            }
            
            guard let status = reslut as? [String:AnyObject] else {
                finished(isDownLoaded: false)
                return
            }
            
            guard let homeData = status["statuses"] as? [[String:AnyObject]] else {
                finished(isDownLoaded: false)
                return
            }
            
//            将homedata转换为模型
            var tempArray = [SYCellStatusViewModel]()
            for value in homeData{
                let sta = SYStatus(dict: value)
//                print(sta)
                let statusViewModel = SYCellStatusViewModel(status:sta)
                tempArray.append(statusViewModel)
            }
            if isPullUp{
                self.cellStatusViewModel = self.cellStatusViewModel + tempArray
            }else{
                self.cellStatusViewModel = tempArray + self.cellStatusViewModel
            }
            
            finished(isDownLoaded: true)
        }

    }
    


}
